/**
 * Created by zero on 2016/10/28.
 */

$('#myModal').on('hidden.bs.modal', function (e) {
    WeixinJSBridge.call('closeWindow');
})

var appid = $.query.get('appid');
var sa = $.query.get('tip');
if(sa == "zhuce"){
    $(".tip").show();
}else if(sa =="zhuxiao"){
    $("#logout").show();
}

function subtoken() {
    if (sa == "zhuce") {
        bindingToken();
    } else if(sa == "zhuxiao"){
        logout();
    }
}

function bindingToken() {
    var token = $("#token").val();
    $.ajax({
        url: "/user/signtoken",
        type: "post",
        data: {wechatname: appid, token: token},
        success: function (str) {
            if (str == "yes") {
                $(".modal-body").html("绑定成功!");
                $('#myModal').modal('toggle');
            } else if (str == "other") {
                $(".modal-body").html("此token,已绑定其他微信号!");
                $('#myModal').modal('toggle');
            }
            else {
                $(".modal-body").html("绑定失败,请检查token是否正确!");
                $('#myModal').modal('toggle');
            }
        }

    });
}

function logout(){
    $.ajax({
        url:"/user/logout",
        type:"post",
        data:{wechatname:appid},
        success:function(str){
            if(str=="yes"){
                $(".modal-body").html("注销成功!");
                $('#myModal').modal('toggle');
            }else{
                $(".modal-body").html("注销失败,该微信号尚未绑定token!");
                $('#myModal').modal('toggle');
            }
        }

    })
}

function tokenclose(){
    WeixinJSBridge.call('closeWindow');
}